// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import AudioManager from "./Audio";
import FadePanel from "./FadePanel";
import GameData from "./GameData";
import SceneLoad from "./SceneLoad";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UpgradeScene extends cc.Component {

    @property(cc.Node)
    mainPanel: cc.Node;

    @property(cc.Label)
    coinLabel: cc.Label;

    @property(cc.Label)
    needLabel: cc.Label;

    @property(cc.Label)
    mainLabel: cc.Label;

    choose: string;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        FadePanel.Instance.node.children[0].active = true;
        FadePanel.Instance.FadeOut();
        FadePanel.Instance.node.parent.children[2].getComponent(cc.AudioSource).play();
        this.coinLabel.string = GameData.Instance.Datas[0].toString();
    }

    // update (dt) {}

    CheckUpgrade(event, upgrade: string) {
        this.choose = upgrade;
        var datas = GameData.Instance.Upgrades.get(this.choose);
        if (datas[1]) this.ShowUpgraded();
        else this.ShowText(datas);
    }

    private ShowText(datas: Array<number | boolean | string>) {
        this.mainPanel.active = true;
        this.mainLabel.string = datas[2].toString();
        this.needLabel.string = datas[0].toString();
        if (GameData.Instance.Datas[0] > parseInt(this.needLabel.string)) this.needLabel.node.color = cc.Color.WHITE;
        else this.needLabel.node.color = cc.Color.RED;
    }

    private ShowUpgraded() {
        this.mainPanel.active = true;
        this.needLabel.string = "";
        this.mainLabel.string = "已升级";
    }

    Upgrade() {
        var datas = GameData.Instance.Datas;
        var upgrades = GameData.Instance.Upgrades;
        if (datas[0] > parseInt(this.needLabel.string)) {
            datas[0] -= parseInt(this.needLabel.string);
            datas[2]++;
            if (this.choose == "sandwich") datas[4] += 3;
            if (this.choose == "cake") datas[4] += 5;
            if (this.choose == "price") datas[4] += 5;
            upgrades.get(this.choose)[1] = true;
            GameData.Instance.Datas = datas;
            GameData.Instance.Upgrades = upgrades;
            this.ShowUpgraded();
        }
    }

    Back() {
        FadePanel.Instance.FadeIn(() => {
            SceneLoad.GoScene("MainScene");
        })
        AudioManager.Instance.ButtonAudio();
    }
}
